package hit.edu.zjc.Tool;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
 * 
 * 用于判断中文乱码，结果不宜定正确。。。
 * */
public class MessyCodeCheck {

	private static double Threshold=0.3;//非中文字符/总字符数
	
	/*
	 * 返回true，是乱码。
	 * 返回false，不是乱码。
	 * */
	private static boolean CheckMessy(String str)
	{
			str=str.replaceAll("•", "");
			boolean res=java.nio.charset.Charset.forName("gb2312").newEncoder().canEncode(str);
			if(!res)
				return true;
			else 
				return false;
		
	}
	
	
	private static boolean CheckMessy2(String str)
	{

		int count=0;
		for(int i=0;i<str.length();i++)
		{
			if(str.substring(i, i+1).equals("•"))
				continue;
			boolean res=java.nio.charset.Charset.forName("gb2312").newEncoder().canEncode(str.substring(i, i+1));
			if(!res)
				count++;
			
		}
		
		double rate=(1.0*count)/str.length();
		if(rate>Threshold)
			return true;
		else
			return false;
		
	}
	
	
	public static void main(String[] args){
	
		String str1="琛ョ墖锛氫竴浜屼節浜岀増鐓х墖锛堟潨瀛愬▉ 鎽勫奖锛�jpg.jpg";//3260
		String str2="秦裕琨院士“90周年校庆·90名家讲坛”对话学子";//3263
		String str3="";
		System.out.println(isMessyCode(str1));
		//System.out.println(MessyCodeCount(str1));
		System.out.println(isMessyCode(str2));
		System.out.println(isMessyCode(str3));
		//System.out.println(MessyCodeCount(str2));
		/*	
		String str="哈尔滨工业大学";
		String str1="鏂板缓";
		String str2="*��JTP.jar�ļ����JTP�ļ���ȡ��ͼƬ��Դ";
		String str3="∫ ∮ ≡ ≌ ≈ ∽ ?";
		String str4="。。。。你好吗。。。。.....";
		String str5="!!!????!!!???";
		String str6="美国斯坦福大学派瑞 L迈卡蒂教授被我校授予荣誉教授";
		System.out.println(CheckMessy(str));//t
		System.out.println(CheckMessy(str1));//f
		System.out.println(CheckMessy(str2));//f
		System.out.println(CheckMessy(str3));//f
		System.out.println(CheckMessy(str4));//t
		System.out.println(CheckMessy(str5));//t
		System.out.println(CheckMessy(str6));//t
	
		String str="哈尔滨工业大学";
		String str1="鏂板缓";
		String str2="*��JTP.jar�ļ����JTP�ļ���ȡ��ͼƬ��Դ";
		String str3="∫ ∮ ≡ ≌ ≈ ∽ ?";
		
		System.out.println(java.nio.charset.Charset.forName("gb2312").newEncoder().canEncode(str));//true
		System.out.println(java.nio.charset.Charset.forName("gb2312").newEncoder().canEncode(str1));//false
		System.out.println(java.nio.charset.Charset.forName("gb2312").newEncoder().canEncode(str2));//false
		System.out.println(java.nio.charset.Charset.forName("gb2312").newEncoder().canEncode(str3));//true
		System.out.println();
		
		System.out.println(java.nio.charset.Charset.forName("gbk").newEncoder().canEncode(str));//true
		System.out.println(java.nio.charset.Charset.forName("gbk").newEncoder().canEncode(str1));//true
		System.out.println(java.nio.charset.Charset.forName("gbk").newEncoder().canEncode(str2));//false
		System.out.println(java.nio.charset.Charset.forName("gbk").newEncoder().canEncode(str3));//true
		System.out.println();
		
		System.out.println(UsualString(str));//true
		System.out.println(UsualString(str1));//true
		System.out.println(UsualString(str2));//false
		System.out.println(UsualString(str3));//false
		System.out.println();
		
		System.out.println(isMessyCode(str));//false  true
		System.out.println(isMessyCode(str1));//false true
		System.out.println(isMessyCode(str2));//true  false
		System.out.println(isMessyCode(str3));//true  false
		System.out.println();
		
		//鏂
		System.out.println(UsualChinese(str1.charAt(0)));//true
		System.out.println(isChinese(str1.charAt(0)));//true
		System.out.println();
		
		//∫
		System.out.println(UsualChinese(str3.charAt(0)));//false
		System.out.println(isChinese(str3.charAt(0)));//false
		System.out.println();
		
		//" "空格
		String test=" ";
		System.out.println(UsualChinese(test.charAt(0)));//false
		System.out.println(isChinese(test.charAt(0)));//false
		System.out.println();
		
		//","逗号，英文
		test=",";
		System.out.println(UsualChinese(test.charAt(0)));//false
		System.out.println(isChinese(test.charAt(0)));//false
		System.out.println();
		
		//"，"逗号，中文
		test="，";
		System.out.println(UsualChinese(test.charAt(0)));//false
		System.out.println(isChinese(test.charAt(0)));//true
		System.out.println();
		
		//"?"
		test="?";
		System.out.println(UsualChinese(test.charAt(0)));//false
		System.out.println(isChinese(test.charAt(0)));//false
		System.out.println();
		*/
	}
	
	
	private static boolean UsualString(String str)
	{
		int count=0;
		for(int i=0;i<str.length();i++)
		{
			char c=str.charAt(i);
			if(!UsualChinese(c))
				count++;
		}
		double rate=(1.0*count)/str.length();
		if(rate>Threshold)
			return false;
		else
			return true;
	}
	
   /*
    * 通过限定范围的方法
    * 有5%的错误率。
    * 返回true：是常用字
    * 返回false：不是常用字
    * */
	private static boolean UsualChinese(char c)
	{
		   //限定范围
			if(c>=0x4e00 && c<=0x9fa5){ //汉字 
				return true;
			} 
			else
				return false;
		
	}
	
	// GENERAL_PUNCTUATION 判断中文的“号 //
	// CJK_SYMBOLS_AND_PUNCTUATION 判断中文 的。号 /
	// HALFWIDTH_AND_FULLWIDTH_FORMS 判断中文的，号	
	private static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
			return true;
		}
		return false;
	}
	/*
	 * 返回true：是乱码。
	 * 返回false：不是乱码。
	 * */
	public static boolean isMessyCode(String strName) {
	
		Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");  
		Matcher m = p.matcher(strName);   
		String after = m.replaceAll(""); 

		String temp=after.replaceAll("\\p{P}", "");
		char[] ch = temp.trim().toCharArray();
		long chLength=ch.length;

		long count=0;
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if(!Character.isLetterOrDigit(c)){  //确定指定字符是否为字母或数字
			
				if(!isChinese(c)){
					return true;
				}
			}
		}
			return false;

	}
	
	public static int MessyCodeCount(String strName) {
		
		Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");  
		Matcher m = p.matcher(strName);   
		String after = m.replaceAll(""); 

		String temp=after.replaceAll("\\p{P}", "");
		
		char[] ch = temp.trim().toCharArray();
		int chLength=ch.length;

		int count=0;
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if(!Character.isLetterOrDigit(c)){  //确定指定字符是否为字母或数字
			
				if(!isChinese(c)){
					count=count+1;
				}
			}
		}
		return count;

	}

}


